package com.charging.controller;


import com.charging.bean.Advertisement;
import com.charging.bean.Site;
import com.charging.bean.layuiBean.ResultPage;
import com.charging.bean.layuiBean.VoPage;
import com.charging.config.config.BaseController;
import com.charging.config.result.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 小程序广告 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-20
 */
@Controller
@RequestMapping("admin/advertisement")
public class AdvertisementController extends BaseController {


    /**
     * @return 查询
     */
    @RequestMapping(value = "advertisement.html")
    public String advertisement(){
        return "advertisement/advertisement";
    }

    /**
     * @return 添加页面
     */
    @RequestMapping(value = "advertisement_add.html")
    public String advertisement_add(){
        return "advertisement/advertisement_add";
    }

    /**
     * @return 编辑页面
     */
    @RequestMapping(value = "advertisement_edit.html")
    public String advertisement_edit(Model model,Long id){
        model.addAttribute("advertisement",advertisementService.getById(id));
        return "advertisement/advertisement_edit";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Advertisement> selectMyPage(VoPage voPage){
        return advertisementService.selectMyPage(voPage);
    }


    /**
     * @param advertisement
     * @return 添加
     */
    @RequestMapping(value = "saveAdvertisement",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveAdvertisement(@RequestBody Advertisement advertisement){
        if(advertisementService.save(advertisement)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param advertisement
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdAdvertisement",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteByIdAdvertisement(@RequestBody Advertisement advertisement){
        if(advertisementService.removeById(advertisement)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param advertisement
     * @return 修改
     */
    @RequestMapping(value = "updateByIdAdvertisement",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateByIdAdvertisement(@RequestBody Advertisement advertisement){
        if(advertisement.getFlag()==1){
            advertisement.setContent("");
        }
        if(advertisementService.updateById(advertisement)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 根据id下拉框查询信息
     */
    @RequestMapping(value = "selectByIdAdvertisement",method = RequestMethod.GET)
    @ResponseBody
    public Advertisement selectByIdAdvertisement(Long id){
        return advertisementService.getById(id);
    }

}
